import json
import requests
import re


def get(msg):
    # 正则匹配需要查找的词
    pattern = re.compile(r'(.+)的翻译')
    keyWord = pattern.findall(msg)[0]

    # 调用有道翻译API
    response = requests.get(
        url='http://fanyi.youdao.com/translate?&doctype=json&type=AUTO&i=' + keyWord)
    result = json.loads(response.text)

    return '“' + keyWord + '” 的翻译是：\n' + result['translateResult'][0][0]['tgt']


if __name__ == '__main__':
    print(get('测试的翻译'))
